// ----------------------------------------------------------------------------------
// Ajax Paging List Using jQuery
// ----------------------------------------------------------------------------------
function $A(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) return iterable.toArray();
	var length = iterable.length || 0, results = new Array(length);
	while (length--) results[length] = iterable[length];
	return results;
}

if (typeof(Prototype) != 'undefined' && Prototype.Browser.WebKit) {
$A = function(iterable) {
	if (!iterable) return [];
		// In Safari, only use the `toArray` method if it's not a NodeList.
		// A NodeList is a function, has an function `item` property, and a numeric
		// `length` property. Adapted from Google Doctype.
		if (!(typeof iterable === 'function' && typeof iterable.length === 'number' && typeof iterable.item === 'function') && iterable.toArray)
			return iterable.toArray();
		var length = iterable.length || 0, results = new Array(length);
		while (length--) results[length] = iterable[length];
		return results;
	};
}

Function.prototype.bindAsEventListener = function() {
    var __method = this, args = $A(arguments), object = args.shift();
    return function(event) {
      return __method.apply(object, [event || window.event].concat(args));
    };
};

//----------------------------------------------------------------------------------
// Paging Function
//----------------------------------------------------------------------------------
function myPager(obj, opts) {
	var options =  {
		id: null,
		total: 0,
		display: 10,
	    page: 1,
	    block: 10,
		first: '<span class="first"><a href="#"  style="padding-right: 5px;"><img src="resources/img/btn/pg_first.gif" alt="맨앞 페이지" /></a></span>',
		prev: '<span class="prev"><a href="#"><img src="resources/img/btn/pg_prev.gif" alt="이전 페이지" /></a></span>',
		next: '<span class="next"><a href="#"><img src="resources/img/btn/pg_next.gif" alt="다음 페이지" /></a></span>',
		last: '<span class="last"><a href="#" style="padding-left: 5px;"><img src="resources/img/btn/pg_last.gif" alt="맨끝 페이지" /></a></span>'
	};	
	
	this.options = $.extend(options, opts);
	this.holder = obj;
	this.holder.each(this.render.bindAsEventListener(this));
}

myPager.prototype = {
    hide: function() {
    	this.holder.hide();
    	return this;
    },
    show: function() {
    	this.holder.show();
    	return this;
    },
	render: function(options) {
    	// empty out the destination element and then render out the pager with the supplied options
    	this.holder.empty();
    	
		var opts = $.extend(this.options, options);
		opts.page = parseInt(opts.page);
		
		if(opts.total <= 0) return;

		var pagecount = Math.ceil(opts.total / opts.display);
		
		
		//ul추가
		//this.holder.append('<ul>');
		
	    // add in the previous and next buttons
		if(opts.first) this.holder.append(this.renderFirst(opts, pagecount));
		if(opts.prev) this.holder.append(this.renderPrev(opts, pagecount));
	
	    // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
	    var mid = parseInt(opts.block /2);
	    
	    var startPoint = 1;
	    var endPoint = opts.block;
	
	    if (opts.page > mid) {
	        startPoint = opts.page - mid;
	        endPoint = opts.page + mid;
	    }
	
	    if (endPoint > pagecount) {
	        startPoint = pagecount - opts.block;
	        endPoint = pagecount;
	    }
	    
	    if (startPoint < 1) {
	        startPoint = 1;
	    }
	    
	    // loop thru visible pages and render buttons
	    for (var page = startPoint; page <= endPoint; page++) {
	    	this.renderPage(opts, page);
	    }
	    
	    if(opts.next) this.holder.append(this.renderNext(opts, pagecount));
	    if(opts.last) this.holder.append(this.renderLast(opts, pagecount));
	    //this.holder.append('</ul>');
	    return this.holder;	
	},
	renderPage: function(opts, page) {
   		$('<a href="#"'+((opts.page == page)?' class="current"':'')+'>' + (page) + '</a>').appendTo(this.holder).click(function() { return (opts.page == page)?false:opts.callback(this.firstChild.data); });    		
   		//$('<li><a href="javascript:">' + (page) + '</a>|</li>').appendTo(this.holder).click(function() { return (opts.page == page)?false:opts.callback(page); });    		
	},
	renderFirst: function(opts, pagecount) {
		if(opts.page <= 1)
			return $(opts.first);
		else
			return $(opts.first).click(function() { return opts.callback(1); });
	},
	
	renderPrev: function(opts, pagecount) {
		if(opts.page <= 1) {
			return $(opts.prev);
		} else {
			return $(opts.prev).click(function() { return opts.callback(opts.page-1); });
		}
	},
	
	renderNext: function(opts, pagecount) {
		if(opts.page >= pagecount) {
			return $(opts.next);
		} else {
			return $(opts.next).click(function() { return opts.callback(opts.page+1); });
		}
	},
	
	renderLast: function(opts, pagecount) {
		if(opts.page >= pagecount) {
			return $(opts.last);
		} else {
			return $(opts.last).click(function() { return opts.callback(pagecount); });
		}
	}
};
